package pl.msikora.android.lapmagic.view.overlay;

import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;

/**
 * Overlay item class adapted to represent track point.
 * 
 * @author Maciej Sikora
 *
 */
public class PointOverlayItem extends OverlayItem {
	
	private String name;
	private String type;
	private long id;
	private long radius;
	private boolean changed = false;
	private Location myLocation = null;

	/**
	 * Constructor which provides specific values to every point attribute.
	 */
	public PointOverlayItem(GeoPoint point, String name, String type,
			long id, long radius) {
		super(point, null, null);
		this.name = name;
		this.type = type;
		this.id = id;
		this.radius = radius;
	}

	/**
	 * Constructor for a new default point which isn't connected with any track.
	 */
	public PointOverlayItem(GeoPoint point, String name, String type) {
		super(point, null, null);
		this.name = name;
		this.type = type;
		this.id = -1;
		this.radius = 15;
	}
	
	public Location getLocation() {
		return myLocation;
	}
		
	public String getName() {
		return name;
	}
	
	public String getType() {
		return type;
	}
	
	public long getId() {
		return id;
	}
	
	public long getRadius() {
		return radius;
	}

	/**
	 * Checks if the point has changed since its creation on the map.
	 * This tells if we should update the database or not.
	 * 
	 * @return true if point changed, false if not.
	 */
	public boolean hasChanged() {
		return changed;
	}
	
	/**
	 * Sets Location object representing that point.
	 * 
	 * @param provider name of the location provider
	 */
	public void setLocation(String provider) {
		if (myLocation == null) {
			myLocation = new Location(provider);
			GeoPoint point = getPoint();
			myLocation.setLatitude(point.getLatitudeE6() / 1e6);
			myLocation.setLongitude(point.getLongitudeE6() / 1e6);
		}		
	}
	
	public void setName(String name) {
		if (!name.equals(this.name)) {
			this.name = name;
			this.changed = true;
		}
	}

	public void setType(String type) {
		if (!type.equals(this.type)) {
			this.type = type;
			this.changed = true;
		}
	}

	public void setId(long id) {
		if (id != this.id) {
			this.id = id;
			this.changed = true;
		}
	}

	public void setRadius(long radius) {
		if (radius != this.radius) {
			this.radius = radius;
			this.changed = true;
		}
	}
	
	/**
	 * Explicitly marks the point changed. 
	 */
	public void setChanged() {
		changed = true;
	}

}
